package org.community.test.service;

import org.bytedeco.ffmpeg.avcodec.AVPacket;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.*;

import java.io.IOException;
import java.io.InputStream;

public class FFmpegProcessor {

    /**
     * 这个方法的url地址都必须是一样的类型 同为post
     */
    public static boolean convertMediaToM3u8ByHttp(InputStream inputStream, String m3u8Url, String infoUrl,String filePath,String fileNamePrefix) throws IOException {

        avutil.av_log_set_level(avutil.AV_LOG_INFO);
        FFmpegLogCallback.set();

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputStream);
        grabber.start();

        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(m3u8Url, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());



        recorder.setFormat("hls");
        recorder.setOption("hls_time", "5");
        //不根据gop间隔进行切片,强制使用hls_time时间进行切割ts分片
//        recorder.setOption("hls_flags", "split_by_time");
        recorder.setOption("hls_list_size", "0");

        recorder.setOption("hls_flags", "delete_segments");

        recorder.setOption("hls_delete_threshold", "1");
        recorder.setOption("hls_segment_type", "mpegts");
        recorder.setOption("hls_segment_filename", filePath+fileNamePrefix+"_%d.ts");

        if(infoUrl != null){
            recorder.setOption("hls_key_info_file", infoUrl);
        }
//

        // http属性
        recorder.setOption("method", "POST");

//        recorder.setVideoOption("tune", "fastdecode");
//        // 快速
//        recorder.setVideoOption("preset", "ultrafast");
////        recorder.setVideoOption("crf", "26");
//        recorder.setVideoOption("threads", "12");
//        recorder.setVideoOption("vsync", "2");
//        recorder.setFrameRate(grabber.getFrameRate());// 设置帧率


        recorder.setFrameRate(25);
        recorder.setGopSize(2 * 25);
        recorder.setVideoQuality(1.0);
        recorder.setVideoBitrate(10 * 1024);
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
//        recorder.start();
//
//        Frame frame;
//        while ((frame = grabber.grabImage()) != null) {
//            try {
//                recorder.record(frame);
//            } catch (FrameRecorder.Exception e) {
//                e.printStackTrace();
//            }
//        }
//        recorder.setTimestamp(grabber.getTimestamp());
//        recorder.close();
//        grabber.close();

        recorder.start(grabber.getFormatContext());
        AVPacket packet;
        while ((packet = grabber.grabPacket()) != null) {
            try {
                recorder.recordPacket(packet);
            } catch (FrameRecorder.Exception e) {
             e.printStackTrace();
            }
        }
        recorder.setTimestamp(grabber.getTimestamp());
        recorder.stop();
        recorder.release();
        grabber.stop();
        grabber.release();
        return true;

    }

    public static boolean convertMediaToM3u8ByHttp(InputStream inputStream, String m3u8Url,String filePath,String fileNamePrefix) throws IOException {
        return convertMediaToM3u8ByHttp(inputStream,m3u8Url,null,filePath,fileNamePrefix);
    }

}
